package com.zuiacsn.forum.service.impl;

import com.zuiacsn.forum.common.AppResult;
import com.zuiacsn.forum.common.ResultCode;
import com.zuiacsn.forum.dao.BoardMapper;
import com.zuiacsn.forum.exception.ApplicationException;
import com.zuiacsn.forum.model.Board;
import com.zuiacsn.forum.service.IBoardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.transform.Result;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class BoardServiceImpl implements IBoardService {

    @Autowired
    BoardMapper boardMapper;

    /**
     * 根据num查询
     * @param num
     * @return
     */
    @Override
    public List<Board> selectByNum(Integer num) {
        // 非空校验
        if(num <= 0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        // 丛数据库中进行查询
        List<Board> boards = boardMapper.selectByNum(num);
        return boards;
    }

    /**
     * 更新板块中帖子数量
     * @param id
     */
    @Override
    public void addOneArticleCountById(Long id) {
        // 非空检验
        if(id == null || id <= 0){
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }

        Board board = boardMapper.selectByPrimaryKey(id);

        // 校验板块是否为空
        if(board == null){
            log.warn(ResultCode.ERROR_IS_NULL.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }

        // 更新帖子数量:  板块帖子数量加1
        Board updateBoard = new Board();
        updateBoard.setArticleCount(board.getArticleCount()+1);
        updateBoard.setId(board.getId());
        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);
        if(row != 1){
            log.warn(ResultCode.FAILED.toString()+", 受影响的行数不等于1");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }

    @Override
    public Board selectById(Long id) {
        // 非空检验
        if(id == null || id <= 0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        Board board = boardMapper.selectByPrimaryKey(id);
        return board;
    }

    @Override
    public void subOneArticleCountById(Long id) {
        // 非空检验
        if(id == null || id <= 0){
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }

        Board board = boardMapper.selectByPrimaryKey(id);

        // 校验板块是否为空
        if(board == null){
            log.warn(ResultCode.ERROR_IS_NULL.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }

        // 更新帖子数量:  板块帖子数量加减一
        Board updateBoard = new Board();
        updateBoard.setArticleCount(board.getArticleCount()-1);
        updateBoard.setId(board.getId());
        if(updateBoard.getArticleCount() <= 0){
            updateBoard.setArticleCount(0);
        }

        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);
        if(row != 1){
            log.warn(ResultCode.FAILED.toString()+", 受影响的行数不等于1");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }
}
